package com.qf.servlet;

import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/imgServlet")
public class ImgServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf8");
        String pic = request.getParameter("picture");
        if (StringUtil.isEmpty(pic)){
            return;
        }
        String basePath = this.getServletContext().getRealPath("WEB-INF"+ File.separator+"images"+File.separator+pic);
        File file = new File(basePath);
        if (file.exists()){//
            FileInputStream fis = new FileInputStream(file);
            ServletOutputStream os = response.getOutputStream();
            byte[] buf = new byte[1024*8];
            int len;
            while ((len=fis.read(buf)) != -1){
                os.write(buf,0,len);
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}